package com.oanda.blackberry.util;

import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.util.Persistable;

import com.oanda.blackberry.restapi.Account;
import com.oanda.blackberry.restapi.Rate;
import com.oanda.blackberry.util.log.Logger;

public class Settings implements Persistable {
    private static Settings SINGLETONINSTANCE;
    private int urlIndex = 0;
    private UserSettings[] userAccountSettings;
    static final int maxUrls = 2;
    String sessionToken;
    int accountEntriesIndex = -1;
    Account account;
    Rate rate;
    boolean trustAll;
    boolean enableHttpsRedirec;
    boolean endToEndRequired;
    public int connectionSuffixIndex = 0;

    public String historyTimeInterval = " 1 min ";

    public int historyChartTypeIndex = 0;

    public boolean showInternetTrafficWarning = true;

    public int getConnectionSuffixIndex() {
        return this.connectionSuffixIndex;
    }

    public void setConnectionSuffixIndex(int connectionSuffixIndex) {
        this.connectionSuffixIndex = connectionSuffixIndex;
    }

    public int getAccountEntriesIndex() {
        return this.accountEntriesIndex;
    }

    public void setAccountEntriesIndex(int accountEntriesIndex) {
        this.accountEntriesIndex = accountEntriesIndex;
    }

    public Rate getRate() {
        if (this.rate == null) {
            this.rate = new Rate();
        }
        return this.rate;
    }

    public void setRate(Rate rate) {
        this.rate = rate;
    }

    public Account getAccount() {
        return this.account;
    }

    public UserSettings getUserAccountSettings(int index) {
        if ((this.userAccountSettings != null) && (this.userAccountSettings.length > index))
            return this.userAccountSettings[index];
        return null;
    }

    public void setUserAccountSettings(UserSettings userSettings, int index) {
        this.userAccountSettings[index] = userSettings;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    public Settings() {
        this.userAccountSettings = new UserSettings[2];
        for (int i = 0; i < 2; i++)
            this.userAccountSettings[i] = new UserSettings(i, null, null, false);
    }

    private Settings(String dummy) {
        PersistentObject persist = PersistentStore.getPersistentObject(200164990982444352L);
        Object tempModel = persist.getContents();

        if ((tempModel != null) && ((tempModel instanceof Settings))) {
            Logger.log("tempModel != null && tempModel instanceof Settings");
            SINGLETONINSTANCE = (Settings) tempModel;
        } else {
            SINGLETONINSTANCE = new Settings();
        }
    }

    public static Settings getSettings() {
        if (SINGLETONINSTANCE == null) {
            new Settings("dummy");
        }
        return SINGLETONINSTANCE;
    }

    public int getUrlIndex() {
        return this.urlIndex;
    }

    public void setUrlIndex(int index) {
        Logger.log("setting urlIndex: " + String.valueOf(index));
        this.urlIndex = index;
    }

    public String getSessionToken() {
        return this.sessionToken;
    }

    public void setSessionToken(String sessionToken) {
        this.sessionToken = sessionToken;
    }

    public void commit() {
        Logger.log("commit: " + String.valueOf(this.urlIndex));
        Logger.log("commit: " + String.valueOf(getAccountEntriesIndex()));
        PersistentObject persist = PersistentStore.getPersistentObject(200164990982444352L);
        persist.setContents(SINGLETONINSTANCE);
        persist.commit();
    }

    public boolean isEnableHttpsRedirec() {
        return this.enableHttpsRedirec;
    }

    public boolean isTrustAll() {
        return this.trustAll;
    }

    public void setEnableHttpsRedirec(boolean enableHttpsRedirec) {
        this.enableHttpsRedirec = enableHttpsRedirec;
    }

    public void setTrustAll(boolean trustAll) {
        this.trustAll = trustAll;
    }

    public void setEndToEndRequired(boolean endToEndRequired) {
        this.endToEndRequired = endToEndRequired;
    }

    public boolean isEndToEndRequired() {
        return this.endToEndRequired;
    }

    public int getHistoryChartTypeIndex() {
        return this.historyChartTypeIndex;
    }

    public String getHistoryTimeInterval() {
        return this.historyTimeInterval;
    }

    public void setHistoryChartTypeIndex(int historyChartTypeIndex) {
        this.historyChartTypeIndex = historyChartTypeIndex;
    }

    public void setHistoryTimeInterval(String historyTimeInterval) {
        this.historyTimeInterval = historyTimeInterval;
    }

    public boolean isShowInternetTrafficWarning() {
        return this.showInternetTrafficWarning;
    }

    public void setShowInternetTrafficWarning(boolean showInternetTrafficWarning) {
        this.showInternetTrafficWarning = showInternetTrafficWarning;
    }
}
